package com.livra.examples.pickwebvoter.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.livra.authentication.State;

import oauth.signpost.OAuth;

@SuppressWarnings("serial")
public class LoginServlet extends AbstractPickWebVoterServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        if (req.getParameter(ACTION_PARAMETER) == null) {
            if (this.livraService.getAuthenticationState() == State.INITIAL) {
                this.livraService.authenticateStepOne();
                resp.sendRedirect(OAuth.addQueryParameters(this.livraService
                        .getLogInUrl(), OAuth.OAUTH_CALLBACK, req
                        .getRequestURL().toString()));
                return;
            }
            this.livraService.authenticateStepThree();
            req.getRequestDispatcher(ServletConstants.MAIN_VIEW).forward(req, resp);
            return;
        }
        if (isAction(ServletConstants.SIGNOUT_ACTION)) {
            this.livraService.logout();
            req.getRequestDispatcher(ServletConstants.LOGIN_VIEW).forward(req,resp);
            return;
        }
    }

}
